/**
 *
 * User: wangshan
 * Email wangshan@fclassroom.com
 * Date: 17-11-27
 * Time: PM 01:03
 * Explain: 项目中axios方式调用增删改查
 *
 **/
import axios from 'axios'
import {Notice} from 'iview'
import store from '@/store'

let client = new ApiClient('')
let errHtml = ''
const _ENV = process.env.NODE_ENV === 'development'

/**
 *
 * User: wangshan
 * Email wangshan@fclassroom.com
 * Date: 18-02-02
 * Time: AM 09:14
 * Explain: 接口报错信息拦截展示
 *
 **/
function errorStatement(status) {
  let statement = ''

  if (status === 404) {
    statement = status + '： 无法找到请求的资源'
    return statement
  }
  if (status === 403) {
    statement = status + '： 请求资源的访问被服务器拒绝'
    return statement
  }
  if ((status + '').indexOf('5') > -1) {
    statement = status + '： 接口调用失败，查看接口返回'
    return statement
  }
}

function errorInfo(e) {
  let config = e.config
  let response = e.response
  errHtml = '请求地址：' + '<a style=color:blue>' + config.url + '</a>'
  errHtml = errHtml + '</br>' + '请求方式：' + '<b style=font-weight:bold>' + config.method + '</b>'
  errHtml = errHtml + '</br>' + '错误信息：' + '<b style=font-weight:bold>' + response.statusText + '</b>'
  errHtml = errHtml + '</br>' + '报错说明：' + '<span style=color:red>' + errorStatement(response.status) + '</span>'
  return errHtml
}

/**
 *
 * User: wangshan
 * Email wangshan@fclassroom.com
 * Date: 17-11-27
 * Time: AM 09:14
 * Explain: 项目接口超时处理，消息头请求之前对请求数据做处理
 *
 **/

function ApiClient(baseUrl) {
  // 自定义超时和数据序列化处理
  this.axiosInstance = axios.create({
    baseURL: baseUrl,
    timeout: 30000
  })
  // 请求拦截处理
  this.axiosInstance.interceptors.request.use((config) => {
    if (store.state.token) {
      config.headers['Authorization'] = 'Bearer ' + store.state.token
    }
    config.headers['Content-Type'] = 'application/json'
    config.headers['Client-Value'] = 14
    return config
  })
  // 返回拦截处理
  this.axiosInstance.interceptors.response.use((res) => {
    if (res.data && res.status === 200 && res.data.code === 4) {
      // MessageBox.confirm('您的账号已在别处登录，如果想继续浏览，请您重新登录')
      return
    }
    if (res.status !== 200 || res.data.code === 6 || res.data.code === 10 || res.data.code === 70001) {
      // Notice.error({
      //   title: '报错了：' + res.data.code,
      //   desc: res.data.message
      // })
      return false
    }
    if (res.data && (res.status === 200)) {
      return res
    }
  })
}

/**
 *
 * User: wangshan
 * Email wangshan@fclassroom.com
 * Date: 17-11-27
 * Time: AM 09:17
 * Explain:项目中post提交接口
 *
 **/

ApiClient.prototype.post = function (url, params) {
  return new Promise((resolve, reject) => {
    return this.axiosInstance.post(url, params).then(response => {
      if (response) {
        resolve(response)
      }
    }).catch((error) => {
      if (error.message === 'Network Error') {
        reject(error)
      } else {
        console.log(JSON.stringify(errorInfo(error)))
        if (_ENV) {
          Notice.error({
            title: '出错了！',
            desc: JSON.stringify(errorInfo(error))
          })
        }
      }
    })
  })
}

export default client
